﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Formality.Logic.Game.Database;

namespace Formality.Logic.Game
{
    using db = Database;
    using Formality.FormalSystem;

    public class Player
    {
        public Player(string Name)
        {
            db.Player PlayerRecord;

            this.Name = Name;

            using (var Context = new FormalityDataContext())
            {
                PlayerRecord = (from P in Context.Players
                                where P.Name == Name
                                select P).SingleOrDefault();

                if (PlayerRecord == null)
                {
                    PlayerRecord = new db.Player
                    {
                        Name = Name,
                        Problem = "*2.01."
                    };

                    Context.Players.InsertOnSubmit(PlayerRecord);

                    Context.SubmitChanges();
                }
            }

            PlayerId = PlayerRecord.PlayerId;
        }

        public string ProblemId { get { return PlayerRecord.Problem; } }
        public string Name { get; private set; }
        public IEnumerable<string> CurrentProblemSubmissions
        {
            get { return ProblemSubmissions(ProblemId); }
        }

        public IEnumerable<string> ProblemSubmissions(string ProblemId)
        {
            using (var Context = new FormalityDataContext())
            {
                var Player = GetPlayerRecord(Context);

                return (from S in Player.Submissions
                        where S.ProblemId == ProblemId
                        orderby S.SubmissionOrder
                        select S.Sentence).ToList();
            }
        }

        public void AddSubmission(Sentence Submission, int Order)
        {
            using (var Context = new FormalityDataContext())
            {
                var Record = new db.Submission
                {
                    PlayerId = PlayerId,
                    ProblemId = ProblemId,
                    Sentence = Submission.ToString(),
                    SubmissionOrder = Order
                };

                Context.Submissions.InsertOnSubmit(Record);

                Context.SubmitChanges();
            }
        }

        public void ChangeGoal(string ProblemId)
        {
            using (var Context = new FormalityDataContext())
            {
                var Rec = GetPlayerRecord(Context);

                Rec.Problem = ProblemId;

                Context.SubmitChanges();
            }
        }

        private db.Player PlayerRecord
        {
            get
            {
                using (var Context = new FormalityDataContext())
                    return GetPlayerRecord(Context);
            }
        }

        private db.Player GetPlayerRecord(FormalityDataContext Context)
        {
            return (from P in Context.Players
                    where P.PlayerId == PlayerId
                    select P).Single();
        }

        private int PlayerId;
    }
}
